package http

import (
	"bufio"
	"io"
	"net/http"
	"net/url"
)

//http[get] 方法 return 返回内容,内容字节数,错误
func HttpGet(URL string, Params url.Values) (Result string,Length int,Err error) {
	if parseURL, err := url.Parse(URL); err == nil {
		//如果参数中有中文参数,这个方法会进行URLEncode
		parseURL.RawQuery = Params.Encode()
		urlPathWithParams := parseURL.String()
		resp, err := http.Get(urlPathWithParams)
		if err != nil {
			return "",0, err
		}
		defer resp.Body.Close()

		data := make([]byte, 1025)
		r := bufio.NewReader(resp.Body)
		n, err := r.Read(data)
		if err != nil {
			return "",0, err
		}
		return string(data),n, nil
	} else {
		return "",0, err
	}
}

//http[post] 方法 return 返回内容,内容字节数,错误
func HttpPost_raw(URL string, Body io.Reader) (Result string,Length int,Err error) {
	resp, err := http.Post(URL,"application/json; charset=UTF-8",Body)
	if err != nil {
		return "",0,err
	}
	defer resp.Body.Close()
	data := make([]byte, 1025)
	r := bufio.NewReader(resp.Body)
	n, err := r.Read(data)
	if err != nil {
		return "",0,err
	}
	return string(data),n, nil
}
